﻿#if NET45
using System.ComponentModel;
using System.Configuration;

namespace Longbow.Cache
{
    /// <summary>
    /// CacheSection 操作类
    /// </summary>
    internal class CacheSection : ConfigurationSection
    {
        /// <summary>
        /// 默认配置小节项名称，默认是cacheManager
        /// </summary>
        public const string DefaultSectionName = "cacheManager";

        /// <summary>
        /// 获得 是否启用本设置，默认为启用(true)
        /// </summary>
        [ConfigurationProperty("enabled"), DefaultValue(true)]
        public bool Enabled
        {
            get { return (bool)this["enabled"]; }
        }

        /// <summary>
        /// 获得 CacheOption 集合
        /// </summary>
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public CacheOptionCollection CacheOptions
        {
            get { return (CacheOptionCollection)this[""]; }
        }

        /// <summary>
        /// 获取 CacheSection 配置项方法
        /// </summary>
        /// <returns></returns>
        public static CacheSection GetSection()
        {
            return ConfigurationManager.GetSection(DefaultSectionName) as CacheSection;
        }

        /// <summary>
        /// 获取 CacheOptions 方法
        /// </summary>
        /// <returns>CacheOptions 实例</returns>
        public static CacheOptions GetOptions()
        {
            var options = new CacheOptions() { Enabled = true };

            var section = GetSection();
            if (section.Enabled) options.CacheItems = section.CacheOptions;
            else options.CacheItems = new CacheOption[0];

            var corsSection = CacheListSection.GetSection();
            if (corsSection.Enabled) options.CorsItems = corsSection.CorsItems;
            else options.CorsItems = new CacheCorsItem[0];

            return options;
        }
    }
}
#endif
